package com.fairytask.ui
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.resource.StaticIni;
	import com.jzonl.logic.BattleLogic;
	import com.jzonl.managers.TipsManage;
	import com.jzonl.net.messages.CustomToServerFlag;
	import com.jzonl.net.mgr.NetMgr;
	import com.jzonl.stage.battle.BattleEvent;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.events.MouseEvent;
	
	import fairy.util.easing.Elastic;
	import fairy.util.easing.TweenUtil;
	
	/**
	 * 战斗结算界面 
	 * @author xujh
	 * 
	 */
	public class BattleResultUI extends FrameUI
	{
		private static var _self	:BattleResultUI;
		
		public var sprTit		:Clip;		//获得灵力
		public var itemTit		:Clip;		//获得物品
		public var expLab		:Label;
		public var sprLab		:Label;
		public var expTit		:Clip;		//获得经验
		public var replayBtn	:Button;	//回看
		public var closeBtn	:Button;	//离开
		public var resultTxt	:Clip = new Clip();		//战斗结果
		
//		private var _titleMc	:Clip;		//战斗结果图片，胜利/失败
		//获得物品
		private var _itemArr	:Array	=	[];
		//物品显示位置控制135 130
		private const _itemInitX	:int	=	40;
		private const _itemInitY	:int	=	95;
		
		public static function getInstance():BattleResultUI
		{
			if(_self == null)
			{
				_self	=	new BattleResultUI();				
			}
			return _self;
		}
		
		public function BattleResultUI(popUI:Boolean=true)
		{
			super(popUI);
//			_titleMc	=	new Clip(true,onLoaded);
			//会看
			replayBtn.addEventListener(MouseEvent.CLICK,onReplay);
			//离开
			closeBtn.addEventListener(MouseEvent.CLICK,onHide);
		}
		
		/**
		 * 加载完添加到界面 
		 * @param val
		 * 
		 */
		/*private function onLoaded(val:DisplayObject):void
		{
			_titleMc.x	=	100;
			_titleMc.y	=	-10;
			windowBG.addChild(_titleMc);
		}*/
		
		
		/**
		 * 点击回看，重播 
		 * @param evt
		 * 
		 */
		private function onReplay(evt:MouseEvent):void
		{
			model.dispatchEvent(new BattleEvent(BattleEvent.LEAVE_BATTLE,1));
			hide();
		}
		
		/**
		 * 点击离开 
		 * @param evt
		 * 
		 */
		private function onHide(evt:MouseEvent):void
		{
			model.dispatchEvent(new BattleEvent(BattleEvent.LEAVE_BATTLE));
			hide();
		}
		
		/**
		 * 显示结算结果 
		 * @param pList
		 */
		public function showResult(pData:Object):void
		{
			//如果包含就移除
			/*if(windowBG.contains(_titleMc))
			{
				windowBG.removeChild(_titleMc);
			}*/
			var tmpClip	:Clip;
			while(_itemArr.length>0)
			{
				tmpClip	=	_itemArr.shift();
				tmpClip.parent.removeChild(tmpClip);
			}
			
			//战斗胜负
			/*if(pData.win==1)
			{
				//胜利
				_titleMc.source	= ResConfig.pngResPath + "victory.png";
			}
			else
			{
				//失败
				_titleMc.source	= ResConfig.pngResPath + "lose.png";
			}*/
			setResultText(pData.win,pData.result);
			//胜利
			expLab.htmlStr = pData.exp;
			sprLab.htmlStr = pData.spirit;
			var configID	:int;
			while(pData.item.length>0)
			{
				configID	=	pData.item[0].id;
				pData.item.shift();
				tmpClip	=	new Clip();
				tmpClip.source	=	ResConfig.iconPath + StaticIni.getIniVar(configID, "Icon") + ResConfig.iconImg; 
				tmpClip.name = String(configID);
				tmpClip.x	=	_itemInitX+_itemArr.length*60;
				tmpClip.y	=	_itemInitY;
				tmpClip.addEventListener(MouseEvent.ROLL_OVER,onOver);
				tmpClip.addEventListener(MouseEvent.ROLL_OUT,onOut);
				windowBG.addChild(tmpClip);
				_itemArr.push(tmpClip);
			}
			show();
		}
		
		/**
		 * 显示界面 重写并加入动画 
		 * @param space 显示层次
		 * @param onshow 显示后回调
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
			
			this.x	=	(GameStage.rootW-this.width)/2;
			this.y	=	-500;//(GameStage.rootH-this.height)/2;
			
			//加入动画
			TweenUtil.to(this,1000,{x:initX,y:initY,ease:Elastic.easeInOut});
		}
		
		/**
		 * 设置战斗结果文字（完胜-惨败、大胜-大败、险胜-惜败） 
		 * 
		 */
		private function setResultText(win:int,result:int):void
		{
			//11 险胜 12 大胜 13 完胜 01 险败 02 大败 03 惨败
			resultTxt.source = ResConfig.battlePath + win + result + ".png";
			/*switch(type)
			{
				case 11:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000531");
					break;
				case 12:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000529");
					break;
				case 13:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000527");
					break;
				case 01:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000532");
					break;
				case 02:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000530");
					break;
				case 03:
					resultTxt.htmlStrByBorder = StaticIni.getSwfLangStr("1000000528");
					break;
			}*/
		}
		
		/**
		 * 鼠标移过 
		 * @param evt
		 * @param idx
		 * 
		 */
		private function onOver(evt:MouseEvent):void
		{
			if(null!=evt.currentTarget)
			{
				TipsManage.ShowItemTips(uint(evt.currentTarget.name));
			}
		}
		
		private function onOut(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
	}
}